package com.jfinal.enjoy.parser

import com.intellij.lang.ASTNode
import com.intellij.lang.ParserDefinition
import com.intellij.lang.PsiParser
import com.intellij.lexer.FlexAdapter
import com.intellij.lexer.Lexer
import com.intellij.openapi.project.Project
import com.intellij.psi.FileViewProvider
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.TokenType
import com.intellij.psi.tree.IFileElementType
import com.intellij.psi.tree.TokenSet
import com.jfinal.enjoy.EnjoyLanguage
import com.jfinal.enjoy.lexer.EnjoyFlexLexer
import com.jfinal.enjoy.psi.EnjoyFile
import com.jfinal.enjoy.psi.EnjoyTypes

class EnjoyParserDefinition : ParserDefinition {

    override fun createLexer(project: Project): Lexer {
        return FlexAdapter(EnjoyFlexLexer(null))
    }

    override fun createParser(project: Project): PsiParser = EnjoyParser()

    override fun getFileNodeType(): IFileElementType = FILE

    override fun getWhitespaceTokens(): TokenSet = WHITESPACES

    override fun getCommentTokens(): TokenSet = COMMENTS

    override fun getStringLiteralElements(): TokenSet = STRING_LITERALS

    override fun createElement(node: ASTNode): PsiElement = EnjoyTypes.Factory.createElement(node)

    override fun createFile(viewProvider: FileViewProvider): PsiFile =
        EnjoyFile(viewProvider)

    override fun spaceExistenceTypeBetweenTokens(left: ASTNode, right: ASTNode): ParserDefinition.SpaceRequirements {
        // 根据你的语法需求，选择返回值
        // 一般返回 SpaceRequirements.MAY 或 SpaceRequirements.MUST，或者 SpaceRequirements.MUST_NOT
        return ParserDefinition.SpaceRequirements.MAY
    }
}

val FILE: IFileElementType = IFileElementType(EnjoyLanguage)

val COMMENTS: TokenSet = TokenSet.create(
    EnjoyTypes.LINE_COMMENT_CONTENT,
    EnjoyTypes.BLOCK_COMMENT_CONTENT,
    EnjoyTypes.BLOCK_COMMENT_START,
    EnjoyTypes.BLOCK_COMMENT_END,
    EnjoyTypes.LINE_COMMENT_START
)

val STRING_LITERALS: TokenSet = TokenSet.create(EnjoyTypes.STRING_LITERAL)

val WHITESPACES: TokenSet = TokenSet.create(TokenType.WHITE_SPACE)